package com.doublecheck;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.*;

public class DoubleCheck {
    String inFileString;
    ArrayList<String> libPath = new ArrayList<String>();

    public void init() {
        this.inFileString = "C:\\Users\\罗一\\Desktop\\test.txt";
        this.libPath = null;
        return;
    }

    public void doubleCheck() throws IOException, InterruptedException {
        this.init();
        WayChooser waychooser = new WayChooser();
        int isAuto = waychooser.chooseWay();
        waychooser.dispose();
        if (isAuto == 0) {
            WordGeter thisStudent = new WordGeter(inFileString);
            thisStudent.getwords();
            SimHash thisStudentHash = new SimHash();
            thisStudentHash.getTxtHash(thisStudent);
            WordGeter otherStudent = new WordGeter();
            otherStudent.getwords();
            SimHash otherStudentHash = new SimHash();
            otherStudentHash.getTxtHash(otherStudent);
            if (getDistance(thisStudentHash.strSimHash, otherStudentHash.strSimHash) <= 10)
                JOptionPane.showMessageDialog(null, "这两篇文本相似度过高");
            else
                JOptionPane.showMessageDialog(null, "这两篇文本相似度正常");
        } else {
            File file1 = new File(this.inFileString);
            WordGeter thisStudent = new WordGeter(inFileString);
            thisStudent.getwords();
            SimHash thisStudentHash = new SimHash();
            thisStudentHash.getTxtHash(thisStudent);
            int number = this.libPath.size();
            for (int i = 0; i <= number - 1; i++) {
                File file2 = new File(this.libPath.get(i));
                if (!file1.getName().equals(file2.getName())) {
                    WordGeter otherStudent = new WordGeter(this.libPath.get(i));
                    otherStudent.getwords();
                    SimHash otherStudentHash = new SimHash();
                    otherStudentHash.getTxtHash(otherStudent);
                    if (getDistance(thisStudentHash.strSimHash, otherStudentHash.strSimHash) <= 10)
                        JOptionPane.showMessageDialog(null, "与" + file2.getName() + "这篇文本相似度过高");
                }
            }
        }
        return;
    }

    public int getDistance(String str1, String str2) {
        // 计算两篇文本simhash的海明距离
        int distance = 0;// hamming距离
        if (str1.length() != str2.length()) {
            distance = -1;
        } else {
            for (int i = 0; i < str1.length(); i++) {
                if (str1.charAt(i) != str2.charAt(i)) {
                    distance++;
                }
            }
        }
        return distance;
    }
}

class WayChooser extends JFrame {
    private int isAuto;

    class OkListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            isAuto = 1;
        }
    }

    class NoListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            isAuto = 0;
        }
    }

    public int chooseWay() throws InterruptedException {
        // 确定查重方式
        // 不能用while去等待合法数值，需要通过线程的处理来实现
        isAuto = -1;
        FlowLayout layout = new FlowLayout();
        setLayout(layout);
        this.setTitle("是否自动查重");
        JLabel tip = new JLabel("是否自动查重");
        add(tip);
        this.setLocation(400, 300);
        this.setSize(300, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        JButton okButton = new JButton("是");
        JButton noButton = new JButton("否");
        add(okButton);
        add(noButton);
        OkListener ok = new OkListener();
        NoListener no = new NoListener();
        okButton.addActionListener(ok);
        noButton.addActionListener(no);
        while (true) {
            if (isAuto == -1)
                new Thread().sleep(1000);
            else 
                break;
        }
        return isAuto;
    }
}